/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestor_procesos;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jorge
 * @author Oscar
 */
public class Vprocesos extends javax.swing.JFrame {

     Cprocesos nodo, aux; 
     LinkedList<Cprocesos> ColaListos = new LinkedList<Cprocesos>();
     LinkedList<Cprocesos> ColaEspera = new LinkedList<Cprocesos>();
     LinkedList<Cprocesos> ColaTerminado = new LinkedList<Cprocesos>();
     
     int Iteraciones, quantu, Pid, TimeEsp, filas, Trestante,  i , j= 0;
     
     String Nombre, Estado = "";
     
    
    
    public Vprocesos() {
        initComponents();
    }
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        Tquantum = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        Tpid = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        Tnombre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        Titeraciones = new javax.swing.JTextField();
        BAgregarProceso = new javax.swing.JButton();
        Simulacion = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaListos = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        BarraEjecucion = new javax.swing.JProgressBar();
        jLabel7 = new javax.swing.JLabel();
        Lprocesp = new javax.swing.JLabel();
        quant = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        TablaEspera = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        TablaTerminados = new javax.swing.JTable();
        jLabel11 = new javax.swing.JLabel();
        contadorP = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setText("QUANTUM");

        Tquantum.setName("TQuantum"); // NOI18N

        jLabel2.setText("PID");

        Tpid.setName("Tpid"); // NOI18N

        jLabel3.setText("Nombre");

        Tnombre.setName("Tnombre"); // NOI18N

        jLabel4.setText("# Iteraciones");

        BAgregarProceso.setText("AGREGAR PROCESO");
        BAgregarProceso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BAgregarProcesoActionPerformed(evt);
            }
        });

        Simulacion.setText("SIMULACION");
        Simulacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SimulacionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(Tquantum, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(126, 126, 126)
                                .addComponent(jLabel2))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(117, 117, 117)
                                .addComponent(Tpid, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(jLabel3))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(Tnombre, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(Titeraciones, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(223, 223, 223)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(Simulacion))
                            .addComponent(BAgregarProceso))))
                .addContainerGap(57, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Tquantum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(Tpid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Tnombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Titeraciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BAgregarProceso)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Simulacion)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        TablaListos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "PID", "Nombre", "Iteraciones", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TablaListos.setName("TablaListo"); // NOI18N
        jScrollPane1.setViewportView(TablaListos);
        if (TablaListos.getColumnModel().getColumnCount() > 0) {
            TablaListos.getColumnModel().getColumn(0).setResizable(false);
            TablaListos.getColumnModel().getColumn(1).setResizable(false);
            TablaListos.getColumnModel().getColumn(2).setResizable(false);
            TablaListos.getColumnModel().getColumn(3).setResizable(false);
        }

        jLabel5.setText("PROCESOS LISTOS");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addGap(127, 127, 127))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel3.setBorder(new javax.swing.border.MatteBorder(null));

        jLabel6.setText("EN EJECUCION");

        jLabel7.setText("Progreso");

        Lprocesp.setText("Proceso");
        Lprocesp.setName("Lproceso"); // NOI18N

        quant.setText("QUANTUM");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel7)
                .addGap(18, 18, 18)
                .addComponent(BarraEjecucion, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(Lprocesp)
                        .addGap(84, 84, 84))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(64, 64, 64)))
                .addComponent(quant)
                .addGap(22, 22, 22))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(quant))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Lprocesp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(BarraEjecucion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(0, 41, Short.MAX_VALUE))
        );

        jPanel4.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        TablaEspera.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "PID", "Nombre", "Restante", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TablaEspera.setName("TablaEspera"); // NOI18N
        jScrollPane2.setViewportView(TablaEspera);
        if (TablaEspera.getColumnModel().getColumnCount() > 0) {
            TablaEspera.getColumnModel().getColumn(0).setResizable(false);
            TablaEspera.getColumnModel().getColumn(1).setResizable(false);
            TablaEspera.getColumnModel().getColumn(2).setResizable(false);
            TablaEspera.getColumnModel().getColumn(3).setResizable(false);
        }

        jLabel9.setText("PROCESOS EN ESPERA");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addComponent(jLabel9)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel5.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        TablaTerminados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "PID", "Nombre", "Tiempo de vida", "Estado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TablaTerminados.setName("TablaTerminado"); // NOI18N
        jScrollPane3.setViewportView(TablaTerminados);
        if (TablaTerminados.getColumnModel().getColumnCount() > 0) {
            TablaTerminados.getColumnModel().getColumn(0).setResizable(false);
            TablaTerminados.getColumnModel().getColumn(1).setResizable(false);
            TablaTerminados.getColumnModel().getColumn(2).setResizable(false);
            TablaTerminados.getColumnModel().getColumn(3).setResizable(false);
        }

        jLabel11.setText("PROCESOS TERMINADOS");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addComponent(jLabel11)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        contadorP.setText("jLabel8");

        jMenu2.setText("Archivo");

        jMenuItem1.setText("Cargar Procesos");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuItem2.setText("Ejecutar Procesos");
        jMenu2.add(jMenuItem2);

        jMenuBar1.add(jMenu2);

        jMenu1.setText("Salir");
        jMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu1MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(127, 127, 127)
                        .addComponent(contadorP)
                        .addGap(165, 165, 165)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(contadorP)))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        
        JOptionPane.showMessageDialog(rootPane, "Cargando Procesos");
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenu1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu1MouseClicked
       
       
    }//GEN-LAST:event_jMenu1MouseClicked

    private void BAgregarProcesoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BAgregarProcesoActionPerformed
        
        
        llenarDatos();
        nodo.setNombre(Nombre);
        nodo.setPID(Pid);
        nodo.setTejecucion(Iteraciones);
        nodo.setTrestante(Iteraciones);
        nodo.setTespera(tiempo(nodo, ColaListos));
        
        Agregar(nodo, ColaListos, "Listo", true);
        AgregarTabla(TablaListos,ColaListos);
        
    }//GEN-LAST:event_BAgregarProcesoActionPerformed

    public void CargarBarra(int te, int tr){
       int v= 100/te;
        int b = 100 -(v*tr);
    BarraEjecucion.setValue(b);
    BarraEjecucion.repaint();
    }
    
    public int tiempo(Cprocesos nod, LinkedList lis) {
        int suma = 0;
        Cprocesos aux;
        if (lis.isEmpty() != true)
        {
        
            Iterator<Cprocesos> iterador = lis.listIterator();
        
        while (iterador.hasNext()) {
            aux = iterador.next();
            if (aux.PID != nod.getPID()){
                suma+=aux.getTrestante();
            }
        }
        
        } else
            suma = 0;
        
    return suma;
}
    
    private void SimulacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SimulacionActionPerformed
         new Thread(new Hilos()).start();
       quant.setText(String.valueOf(quantu));
    }//GEN-LAST:event_SimulacionActionPerformed
    
    public void Agregar(Cprocesos nod, LinkedList<Cprocesos> cola, String e, boolean ver){
        Cprocesos aux;
        int j = 0;
        
         nod.setEstado(e);
        
        if (cola.isEmpty() != true)
        {
            if (Existe(nod, cola, ver) != true){
             Iterator<Cprocesos> It_Espera = cola.listIterator();
            if (e == "Espera"){
                    
               while (It_Espera.hasNext()){
            
                 aux = It_Espera.next();
            
                 if (nod.getTrestante() >= aux.getTrestante()){
                      cola.add(j, nod);
                      break;
                   } else
                       if (aux == cola.getLast()){
                           cola.add(nod);
                           break;
                    }
                j++;
               }
        
            } else
             cola.add(nod);  
            } else
                JOptionPane.showMessageDialog(rootPane, "El proceso: " + Integer.toString(nod.getPID())+ " Ya existe");
        } else
            cola.addFirst(nod);
        }              
                
    public class Hilos extends Thread {
        public void run() {
            boolean continuar = true;
            Iterator<Cprocesos> N;
            int contar = 0;
            //while (continuar) {
                 LinkedList<Cprocesos> cola = ColaListos;
                 contadorP.setText(String.valueOf(contar));
                while (!cola.isEmpty()){
                    
                    aux = cola.getFirst();
                    Eliminar(cola);
                    jLabel6.setText("Ejecutando");
                    Lprocesp.setText(aux.getNombre());
                          
                      if (aux.getTrestante() > quantu)
                      {
                          for (int tiempo = 1; tiempo <= quantu; tiempo++ ){
                          
                          aux.setTrestante(aux.getTrestante()-1);
                          contar++;
                          contadorP.setText(String.valueOf(contar));
                          
                          CargarBarra(aux.getTejecucion(), aux.getTrestante());
                          
                        try {
                                                    
                            sleep(1000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Vprocesos.class.getName()).log(Level.SEVERE, null, ex);
                        } // fin try-cacth
                          } // Fin For
                          Agregar(aux, ColaEspera, "Espera", false);
                          AgregarTabla(TablaEspera, ColaEspera);
                          AgregarTabla(TablaListos, ColaListos);
                      } else 
                      {
                          while (aux.getTrestante()>0){
                              aux.setTrestante(aux.getTrestante()-1);
                              CargarBarra(aux.getTejecucion(), aux.getTrestante());
                              contar++;
                          contadorP.setText(String.valueOf(contar));
                               try {
                                                    
                            sleep(1000);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Vprocesos.class.getName()).log(Level.SEVERE, null, ex);
                        }
                          }
                          if (aux.getTrestante()==0){
                              Agregar(aux, ColaTerminado, "Terminado", false);
                              AgregarTabla(TablaTerminados, ColaTerminado);
                              AgregarTabla(TablaListos, ColaListos);
                          }
                              
                      }    
                      if (ColaListos.size() == 0){
                          cola=ColaEspera;
                          
                      }
                      contar++;
                } //Fin while Cola
            } 
        }
    //}
    public LinkedList<Cprocesos> Cola(LinkedList<Cprocesos> cola)
    {
          return cola;
    }
    public void AgregarTabla(JTable tabla, LinkedList cola) {
        
        
        DefaultTableModel model = new DefaultTableModel();
               model = (DefaultTableModel) tabla.getModel();
               
        
        filas = 0;
        tabla.setModel(model);
        Iterator<Cprocesos> N = cola.listIterator();
       limpiarTabla(tabla);
        while (N.hasNext()){
        if (tabla.getRowCount() <= cola.size() ){
        model.addRow(new Object[4]);
        }
        aux = N.next();
        tabla.setValueAt(aux.getPID(), filas, 0);
        tabla.setValueAt(aux.getNombre(), filas, 1);
        tabla.setValueAt(aux.getEstado(), filas, 3);    
        
        if (tabla.getName() == "TablaEspera") {
        tabla.setValueAt(aux.getTrestante(), filas, 2);    
        } 
        else 
            if (tabla.getName() == "TablaListos") {
        tabla.setValueAt(aux.getTejecucion(), filas, 2);            
            } else
        tabla.setValueAt(aux.getTejecucion(), filas, 2);                    
       
        filas++;
        }
    }
    
    public void llenarDatos(){
        
        nodo = new Cprocesos();
        Iteraciones = Integer.parseInt(Titeraciones.getText());
        quantu = Integer.parseInt(Tquantum.getText());
        Pid = Integer.parseInt(Tpid.getText());
        Nombre = Tnombre.getText();
        Trestante = Iteraciones;
    }
    
    public boolean Existe(Cprocesos nod, LinkedList<Cprocesos> cola, boolean e) {
             
            boolean esta = false;
            
        Iterator<Cprocesos> It_Espera = cola.listIterator();
            if (e == true){                  
               while (It_Espera.hasNext()){
            
                 aux = It_Espera.next();
            
                 if (nod.getPID() == aux.getPID()){
                      esta = true;
                 }
                 
               }
            }
               return esta;
    } 
   
    public void Eliminar(LinkedList lista ){
         
        if (lista.isEmpty() != true)
        {
            
            lista.removeFirst();
            
        } else
         JOptionPane.showMessageDialog(rootPane, "La Cola esta vacia");
        
    }
    
    public void limpiarTabla(JTable tabla){
        try {
            DefaultTableModel modelo=(DefaultTableModel) tabla.getModel();
            int filas=tabla.getRowCount();
            for (int i = 0;filas>i; i++) {
                modelo.removeRow(0);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error al limpiar la tabla.");
        }
    }
    
              
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Vprocesos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Vprocesos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Vprocesos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Vprocesos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Vprocesos().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BAgregarProceso;
    private javax.swing.JProgressBar BarraEjecucion;
    private javax.swing.JLabel Lprocesp;
    private javax.swing.JButton Simulacion;
    private javax.swing.JTable TablaEspera;
    private javax.swing.JTable TablaListos;
    private javax.swing.JTable TablaTerminados;
    private javax.swing.JTextField Titeraciones;
    private javax.swing.JTextField Tnombre;
    private javax.swing.JTextField Tpid;
    private javax.swing.JTextField Tquantum;
    private javax.swing.JLabel contadorP;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel quant;
    // End of variables declaration//GEN-END:variables
}
